package com.aivin.lib_base.spiders;

import com.aivin.lib_base.bean.HeartBeatTask;
import com.aivin.lib_base.tool.WkHeartBeatTool;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;


public class SpiderTool {
    private WkHeartBeatTool wkHeartBeatTool    ;
    private final int TIMEOUT =10 *1000 ;

    public SpiderTool(){
        wkHeartBeatTool = new WkHeartBeatTool() ;
    }

    public void getChapters(String webUrl , ISpiderCallBack iSpiderCallBack){
        iSpiderCallBack.onStartLoad();
        List<ChapterSpider> chapters= new ArrayList<>() ;
        wkHeartBeatTool.startDelayedAction(new HeartBeatTask() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect(webUrl).timeout(TIMEOUT).get();
                    Elements divs = doc.select("div#list");//查找id为list的div

                    if(divs==null ||divs.size()<1){   // https://www.biquge5200.com/0_111/
                        iSpiderCallBack.onError("getChapters, divs 中没有合适的内容" );
                        return;
                    }
                    Element div=divs.get(0);
                    //WkLog.showMsg("div2="+div.html());

                    Elements dds= div.select("dd") ;//查找所有dd类型的元素
                    if(dds==null ||dds.size()<1){
                        iSpiderCallBack.onError("getChapters,  divs 中没有合适的dd元素" );
                        return;
                    }

                    for(Element dd : dds){
                        //WkLog.showMsg("dd="+dd.html());
                        Element aHref = dd.selectFirst("a") ;// 获取所有标签为a的元素
                        String url= aHref.attr("href");//获取指定属性的值
                        String title= aHref.text();//获取text值
                        chapters.add(new ChapterSpider(url ,title));
                    }

                    iSpiderCallBack.onGetChapters(chapters);

                } catch (Exception e) {
                    iSpiderCallBack.onError("getChapters, "+e.getMessage());
                    e.printStackTrace();
                }
            }
        } ,0);


    }


    public void getPapters(String webUrl ,ISpiderCallBack iSpiderCallBack){
        iSpiderCallBack.onStartLoad();
        StringBuilder sb = new StringBuilder() ;
        wkHeartBeatTool.startDelayedAction(new HeartBeatTask() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect(webUrl).timeout(TIMEOUT).get();
                    Elements divs = doc.select("div#content");

                    if(divs==null ||divs.size()<1){
                        iSpiderCallBack.onError("getPapters ,divs 中没有合适的内容。");
                        return;
                    }
                    Element div=divs.get(0);
                    Elements pps= div.select("p") ;
                    if(pps==null ||pps.size()<1){
                        iSpiderCallBack.onError("getPapters,divs 异常...");
                        return;
                    }

                    for(Element dd : pps){
                        String content= dd.text();
                        sb.append(content);
                        sb.append("\r\n") ;
                    }

                    iSpiderCallBack.onGetContent(sb.toString());

                } catch (Exception e) {
                    iSpiderCallBack.onError("getPapters, "+e.getMessage());
                    e.printStackTrace();
                }
            }
        } ,0);

    }


}
